package cn.song.util;

import cn.hutool.core.util.StrUtil;
import cn.song.entity.excel.ExcelDataModel;
import cn.song.service.ExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Excel工具类
 * 提供统一的Excel操作方法
 *
 * @author SongRenShuo
 * @date 2025/01/06
 */
@Slf4j
@Component
public class ExcelUtils {

    @Autowired
    @Qualifier("easyExcelService")
    private ExcelService easyExcelService;

    @Autowired
    @Qualifier("easyPoiService")
    private ExcelService easyPoiService;

    @Autowired
    @Qualifier("hutoolExcelService")
    private ExcelService hutoolExcelService;

    /**
     * 根据类型获取Excel服务
     *
     * @param type Excel库类型
     * @return Excel服务
     */
    public ExcelService getExcelService(String type) {
        if (StrUtil.isBlank(type)) {
            return easyExcelService;
        }
        
        switch (type.toLowerCase()) {
            case "easypoi":
                return easyPoiService;
            case "hutool":
                return hutoolExcelService;
            case "easyexcel":
            default:
                return easyExcelService;
        }
    }

    /**
     * 验证Excel文件
     *
     * @param file Excel文件
     * @return 是否有效
     */
    public boolean validateExcelFile(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return false;
        }
        
        String fileName = file.getOriginalFilename();
        if (StrUtil.isBlank(fileName)) {
            return false;
        }
        
        // 检查文件扩展名
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        return "xlsx".equals(extension) || "xls".equals(extension);
    }

    /**
     * 读取Excel文件
     *
     * @param file Excel文件
     * @param type Excel库类型
     * @return 数据列表
     * @throws IOException IO异常
     */
    public List<ExcelDataModel> readExcel(MultipartFile file, String type) throws IOException {
        if (!validateExcelFile(file)) {
            throw new IOException("无效的Excel文件");
        }
        
        ExcelService excelService = getExcelService(type);
        return excelService.uploadExcel(file);
    }

    /**
     * 批量处理Excel文件
     *
     * @param files Excel文件列表
     * @param type Excel库类型
     * @return 处理结果
     */
    public List<List<ExcelDataModel>> batchReadExcel(List<MultipartFile> files, String type) {
        List<List<ExcelDataModel>> results = new java.util.ArrayList<>();
        
        for (MultipartFile file : files) {
            try {
                List<ExcelDataModel> dataList = readExcel(file, type);
                results.add(dataList);
            } catch (Exception e) {
                log.error("处理文件{}失败: {}", file.getOriginalFilename(), e.getMessage());
                results.add(new java.util.ArrayList<>());
            }
        }
        
        return results;
    }

    /**
     * 获取支持的Excel库类型
     *
     * @return 支持的Excel库类型列表
     */
    public List<String> getSupportedTypes() {
        return java.util.Arrays.asList("easyexcel", "easypoi", "hutool");
    }

    /**
     * 获取Excel库类型描述
     *
     * @param type Excel库类型
     * @return 类型描述
     */
    public String getTypeDescription(String type) {
        switch (type.toLowerCase()) {
            case "easypoi":
                return "EasyPoi - 基于POI的Excel操作库";
            case "hutool":
                return "Hutool - 国产工具库，包含Excel操作功能";
            case "easyexcel":
            default:
                return "EasyExcel - 阿里巴巴Excel操作库";
        }
    }
} 